package com.qcby.train.mybaitsplusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.train.mybaitsplusdemo.entity.Student;
import com.qcby.train.mybaitsplusdemo.model.ResultJson;
import com.qcby.train.mybaitsplusdemo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: Demo0521Controller
 * @description:
 * @author: lxt
 * @create: 2021-05-21 21:14
 **/
@RestController
@RequestMapping("demo0521")
public class Demo0521Controller {


    @Autowired
    private IStudentService studentService;

    @RequestMapping("listPage0521")
    public ResultJson listPage0521(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(student.getName())){
            // 其实这样写不好
            queryWrapper.like("s.name",student.getName());
        }
        if(student.getClassInfoId() != null){
            // eq equal
            queryWrapper.eq("class_info_id",student.getClassInfo());
        }
        return ResultJson.ok(studentService.listPage0521(page,queryWrapper));
    }

    /**
     *  多对一 left - join
     * @param pageNo
     * @param pageSize
     * @param student
     * @return
     */
    @RequestMapping("listPage052101")
    public ResultJson listPage052101(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        return ResultJson.ok(studentService.listPage0521(page,student));
    }

    /**
     *  多对一  - association
     *  lambda表达式语法
     * @param pageNo
     * @param pageSize
     * @param student
     * @return
     */
    @RequestMapping("listPage052102")
    public ResultJson listPage052102(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(student.getName())){
            queryWrapper.lambda().like(Student::getName,student.getName());
        }
        if(student.getClassInfoId() != null){
            queryWrapper.lambda().eq(Student::getClassInfoId,student.getClassInfoId());
        }
        return ResultJson.ok(studentService.listPage052102(page,queryWrapper));
    }


    @RequestMapping("listPage052103")
    public ResultJson listPage052103(int pageNo,int pageSize,Student student){
        Page<Student> page = new Page<>(pageNo,pageSize);
        return ResultJson.ok(studentService.listPage052103(page,student));
    }

    @RequestMapping("selectByClassInfoId")
    public ResultJson selectByClassInfoId(Long classInfoId){
        return ResultJson.ok(studentService.selectByClassInfoId(classInfoId));
    }

}
